/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.server.rpc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.daikit.commons.server.AbstractDkErrorProcessor;
import com.daikit.commons.shared.exception.DkException;
import com.daikit.daikit4gxt.server.gwtspring.DkGwtRpcRemoteServletWrapper;
import com.sencha.gxt.data.shared.SortDir;


/**
 * Abstract RPC class that all application RPCs should extend.
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class AbstractDkBaseRpcImpl
{

	@Autowired
	private DkGwtRpcRemoteServletWrapper gwtRpcRemoteServletWrapper;
	@Autowired
	private AbstractDkErrorProcessor errorProcessor;

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Handle request and forward it to GwtRpcAdapter
	 *
	 * @param request
	 *           the {@link HttpServletRequest}
	 * @param response
	 *           the {@link HttpServletResponse}
	 * @return null
	 * @throws Exception
	 */
	@RequestMapping(value = "")
	public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception
	{
		onBeforeHandleRequest();
		gwtRpcRemoteServletWrapper.handleRequest(request, response, this);
		return null;
	}

	protected void onBeforeHandleRequest() throws DkException
	{
		// Nothing done by default
	}

	/**
	 * Retrieve current HTTP request.
	 *
	 * @return the current HTTP request.
	 */
	protected final HttpServletRequest getRequest()
	{
		return gwtRpcRemoteServletWrapper.getRequest();
	}

	/**
	 * Retrieve current HTTP response.
	 *
	 * @return the current HTTP response.
	 */
	protected final HttpServletResponse getResponse()
	{
		return gwtRpcRemoteServletWrapper.getResponse();
	}

	/**
	 * Utility method for paging.
	 *
	 * @param sortDir
	 *           the GXT sort direction.
	 * @return {@link Boolean.TRUE} if sort is ascending , {@link Boolean.FALSE} if sort is descending, null otherwise.
	 */
	protected final Boolean isSortAsc(final SortDir sortDir)
	{
		return sortDir == null ? null : sortDir.equals(SortDir.ASC) ? Boolean.TRUE : Boolean.FALSE;
	}

}
